<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Option Navigate Tree Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" href="../core/jquery-ui/jquery-ui.min.css">
<link rel="stylesheet" type="text/css" href="../common/special/navigate-tree/jquery.treeview.css">
<style>
  input { width: 20px; }
  #tree li { cursor: pointer; }
  /* 160 + 160 + 50 + 30 */
  #wrap,
  #sync-task-cover {
    height: 400px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 74px;
    padding: 3px;
  }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../common/ui_utils.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>
<script src="../common/special/navigate-tree/jquery.treeview.min.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // 2000 seconds

// click-option
var createElements = function(div){
  var n = core.randi(2, 7);
  var clickNames = [];
  for(var i=0;i<n;i++) {
    var chname = ui_utils.generateString(2,8);
    var label = div.append('label')
    label.append('input').attr('type', 'radio').attr('id', 'ch'+i).attr('name', 'radio');
    label[0][0].innerHTML += chname;
    div.append('br');
    clickNames.push(chname);
  }
  var ix = core.randi(0, n);
  var qstr = clickNames[ix];
  return {query: qstr, index: ix};
}

// navigate-tree
var FOLDER_TEMPLATE = `<li><span class="folder"></span><ul></ul></li>`
var FILE_TEMPLATE = `<li><span class="file"></span></li>`
var MAX_FILES = 8;

var generateTree = function(neededFiles, currentCount, level, $elem){
  var files = [];
  while(files.length < neededFiles && currentCount < MAX_FILES && level < 2){
    var fileType = core.sample(['file', 'folder']);
    if(fileType === 'file' || level === 2){
      files.push('file');
      $elem.append(FILE_TEMPLATE);
      currentCount += 1;
    } else {
      $elem.append(FOLDER_TEMPLATE);
      var $targetElem = $elem.find('li ul').last();
      var newFiles = core.randi(0, MAX_FILES - files.length -1);
      var nestedFiles = generateTree(newFiles, currentCount+1, level+1, $targetElem);
      currentCount += 1 + newFiles;
      files.push(nestedFiles);
    }
  }
  return files;
}

var drawTree = function($tree){
  var spanElems = $tree.find('span');
  var chosenFile = core.randi(0, spanElems.length-1);
  for(var i=0;i<spanElems.length;i++){
    var name = core.sample(ui_utils.FIFTY_NAMES);
    spanElems[i].innerHTML = name;
    if(i === chosenFile) var expectedName = name;
  }

  $('#tree').treeview({ collapsed: true });
  return expectedName;
}

var createTree = function($tree){
  generateTree(MAX_FILES, 0, 0, $tree);
  var expectedName = drawTree($tree);
  return expectedName;
}

var genProblem = function() {
  // click-option
  var div = d3.select('#boxes');
  div.html('');

  var option_state = -1.0;
  var correctElement = createElements(div);

  d3.select('#subbtn').on('click', function(){
    option_state = d3.select('#ch'+correctElement.index)[0][0].checked ? 1.0 : -1.0;
  });

  // navigate-tree
  var $tree = $('#tree')
  $tree.empty();

  var expectedName = createTree($tree);

  // d3.select('#query').html('Select ' + correctElement.query + ' and click Submit, and then navigate through the file tree. Find and click on the folder or file named "' + expectedName + '".');
  d3.select('#query').html('Navigate through the file tree. Find and click on the folder or file named "' + expectedName + '", after selecting ' + correctElement.query + ' and clicking Submit.');

  $('#tree li').unbind();
  $('#tree li').on('click', function(){
    var fileText = $(this).children('span').text();
    var isFolder = $(this).children('.folder').length === 1;
    // get filename, and end episode if the name matches, or if the item clicked
    // is a file. do not end if it is a folder.
    if(fileText === expectedName) {
      if (option_state == 1.0) {
        core.endEpisode(1, true);
        return false;
      } else {
        core.endEpisode(-1);
        return false;
      }
    } else if(!isFolder) {
      core.endEpisode(-1);
      return false;
    }
  });
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area">
    <div id="boxes"></div>
    <button id="subbtn" class="secondary-action">Submit</button>
    <br>
    <div>
      <ul id="tree" class="filetree"></ul>
    </div>
  </div>
</div>
</body>
</html>
